<?php

namespace App\Transformers;


use App\Models\FsUserHasIm;
use App\Models\Level;
use Illuminate\Support\Facades\Auth;
use League\Fractal\TransformerAbstract;

class FsUserHasImTransformer extends TransformerAbstract
{
    protected $availableIncludes = ['level'];

    protected $hasPermission;

    public function __construct()
    {
        //此处判断权限，返回是否隐藏手机
        $user = Auth::user();
        $res = $user->hasPermissionTo('api.fsuser.phone.all');
        $this->hasPermission = $res;
    }

    public function transform(FsUserHasIm $fsUserHasIm)
    {
        return [
            'id' => $fsUserHasIm->id,
            'add_im_count'=> $fsUserHasIm->add_im_count,
            'fs_user_id' => $fsUserHasIm->fs_user_id,
            'wx' => $this->hasPermission?$fsUserHasIm->wx:substr_name($fsUserHasIm->wx),
            'level_id' => $fsUserHasIm->level_id,
            'phone_num' => $fsUserHasIm->phone_num,
            'call_rate' => $fsUserHasIm->call_rate,
            'sms_num' => $fsUserHasIm->sms_num,
            'created_at' => empty($fsUserHasIm->created_at)?'':$fsUserHasIm->created_at->toDateTimeString(),
            'updated_at' => empty($fsUserHasIm->updated_at)?'':$fsUserHasIm->updated_at->toDateTimeString(),
        ];
    }

    public function includeLevel(FsUserHasIm $fsUserHasIm)
    {
        return $this->nullOrItem($fsUserHasIm->level, new LevelTransformer());
    }

    public function nullOrItem($data, $transformer)
    {
        if (is_null($data)) {
            return parent::null();
        } else {
            return parent::item($data, $transformer);
        }
    }
}